
package org.movee.spring.data.hugegraph.mapping;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.TypeInformation;

public class HugeMappingContext
        extends AbstractMappingContext<BasicHugePersistentEntity<?>, HugePersistentProperty>
        implements ApplicationContextAware {

    // private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context) {
        this.context = context;
    }

    /**
     * testDomain enity all properties
     * @param property property
     * @param owner owner
     * @param simpleTypeHolder simpleTypeHolder
     * @return
     */
    @Override
    protected HugePersistentProperty createPersistentProperty(Property property,
                                                              BasicHugePersistentEntity<?> owner,
                                                              SimpleTypeHolder simpleTypeHolder) {

        // logger.error("GremlinMappingContext createPersistentProperty=============, {}", field.getName());
        return new BasicHugePersistentProperty(property, owner, simpleTypeHolder);
    }

    /**
     * 当第一次通过 mappingcontext 获取 entity 的persistentEntity时，调用这个这个方法
     * @param typeInformation
     * @param <T>
     * @return
     */
    @Override
    protected <T> BasicHugePersistentEntity<T> createPersistentEntity(TypeInformation<T> typeInformation) {

        final BasicHugePersistentEntity<T> entity = new BasicHugePersistentEntity<>(typeInformation);

        if (this.context != null) {
            entity.setApplicationContext(this.context);
        }

        return entity;
    }

}
